﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace LOGIC
{
    public class LogicLodging
    {
        private DB.Database db = new DB.Database();
                
        private DB.LogiTyp lodgingtype;
        private DB.LogiPri lodgingprice;
        private DB.Logi logi;
        public LogicLodging()
        {
        }

        public int veckoNr
        {
            get { return lodgingprice.veckoNr; }
        }
        public string period
        {
            get { return lodgingprice.period; }
        }
        public double pris
        {
            get { return (double)lodgingprice.pris; }            
        }
        

        private LogicLodging(DB.LogiPri lodgingObject2)
        {
            this.lodgingprice = lodgingObject2;
        }

        public string logiID
        {
            get { return logi.logiID; }
        }

        public string logiTyp
        {
            get { return logi.logiTyp; }
        }
        private LogicLodging(DB.Logi lodgingObject3)
        {
            this.logi = lodgingObject3;
        }

        private LogicLodging(DB.LogiTyp lodgingObject)
        {
            this.lodgingtype = lodgingObject;
        }

        private LogicSession session;

        public LogicLodging(LogicSession session)
        {
            this.session = session;
        }

        public string LodgingRoom
        {
            get { return lodgingtype.antalRum; }
        }

        public string LodgingBeds
        {
            get { return lodgingtype.antalBäddar; }
        }

        public string LodgingSquarMeter
        {
            get { return lodgingtype.kvadratmeter; }
        }

        /// <summary>
        /// Metod som hämtar logialternativens olika attribut (antal rum, antal bäddar och kvadratmeter)
        /// </summary>
        /// <param name="tempLodg"></param>
        /// <returns></returns>
        public static LogicLodging GetLodgingInfo(string tempLodg)
        {
            return new LogicLodging(LogicSession.TheSession.Db.GetLodgingInfo(tempLodg));
        }

        /// <summary>
        /// Metod som hämtar de lediga lägenheter som finns utifrån vald vecka och period
        /// </summary>
        /// <param name="s"></param>
        /// <param name="st"></param>
        /// <param name="veckoNr"></param>
        /// <returns></returns>
        public List<string> GetApartmentFreeLodging(DateTime s, DateTime st, string veckoNr) //Returnerar lista över logier
        {
            int antalDagar;
            TimeSpan ts = st - s;
            antalDagar = ts.Days + 1;
            return db.GetApartmentFreeLodging(s, st, veckoNr, antalDagar.ToString()); 
        }
        

        //public List<string> GetCampFreeLodging(DateTime s, DateTime st, string veckoNr, string lodgingType )//Returnerar lista över logier
        //{
        //    int antalDagar;
        //    TimeSpan ts = st - s;
        //    antalDagar = ts.Days + 1;
        //    return db.GetFreeCampLodging(s, st, veckoNr, antalDagar.ToString(), lodgingType.ToString());
        //}

        /// <summary>
        /// Metod som hämtar lediga camping platser för vald vecka och dagar.
        /// </summary>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <param name="weekNr"></param>
        /// <returns></returns>
        public List<string> GetCampFreeLodgin(DateTime startDate, DateTime endDate, int weekNr)
        {
            return db.GetCampFreeLodging(startDate, endDate, weekNr);
        }

        /// <summary>
        /// Metod som hämtar pris för logi
        /// </summary>
        /// <param name="weekNr"></param>
        /// <param name="period"></param>
        /// <returns></returns>
        public IQueryable GetPrice(int weekNr, string period) //Returnerar pris för logi
        {
            return db.GetPrice(weekNr,period);
        }

        /// <summary>
        /// Metod som hämtar logiId för ledig logival
        /// </summary>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <param name="lodgingType"></param>
        /// <returns></returns>
        public List<string> GetLodgingID(DateTime startDate, DateTime endDate, string lodgingType)
        {
            return db.FreeLodgingID(startDate, endDate,lodgingType);
        }

        /// <summary>
        /// Metod som hämtar pris för ett specifikt logialternativ och vecka
        /// </summary>
        /// <param name="tempType"></param>
        /// <param name="tempWeek"></param>
        /// <returns></returns>
        public IQueryable GetLodgingPrice(string tempType, int tempWeek)
        {
            return LogicSession.TheSession.Db.GetLodgingPrice(tempType, tempWeek);
        }

        /// <summary>
        /// Metod som ändrar pris för valt logialternativ för en specifik vecka och period
        /// </summary>
        /// <param name="tempId"></param>
        /// <param name="tempWeek"></param>
        /// <param name="tempPeriod"></param>
        /// <param name="tempPrice"></param>
        public static void ChangedLodgPrice(string tempId, int tempWeek, string tempPeriod, int tempPrice)
        {
            LogicSession.TheSession.Db.ChangedLodgPrice(tempId, tempWeek, tempPeriod, tempPrice);
        }
        



        //public int WeekNumber_Entire4DayWeekRule(DateTime date) //Ska vi ta bort den om vi inte har användning av den??
        //{

        //    const int JAN = 1;
        //    const int DEC = 12;
        //    const int LASTDAYOFDEC = 31;
        //    const int FIRSTDAYOFJAN = 1;
        //    const int THURSDAY = 4;
        //    bool ThursdayFlag = false;

        //    int DayOfYear = date.DayOfYear;

        //    int StartWeekDayOfYear =
        //    (int)(new DateTime(date.Year, JAN, FIRSTDAYOFJAN)).DayOfWeek;
        //    int EndWeekDayOfYear =
        //    (int)(new DateTime(date.Year, DEC, LASTDAYOFDEC)).DayOfWeek;

        //    //StartWeekDayOfYear = StartWeekDayOfYear;
        //    //EndWeekDayOfYear = EndWeekDayOfYear;
        //    if (StartWeekDayOfYear == 0)
        //        StartWeekDayOfYear = 7;
        //    if (EndWeekDayOfYear == 0)
        //        EndWeekDayOfYear = 7;

        //    int DaysInFirstWeek = 8 - (StartWeekDayOfYear);
        //    int DaysInLastWeek = 8 - (EndWeekDayOfYear);

        //    if (StartWeekDayOfYear == THURSDAY || EndWeekDayOfYear == THURSDAY)
        //        ThursdayFlag = true;

        //    int FullWeeks = (int)Math.Ceiling((DayOfYear - (DaysInFirstWeek)) / 7.0);

        //    int WeekNumber = FullWeeks;

        //    if (DaysInFirstWeek >= THURSDAY)
        //        WeekNumber = WeekNumber + 1;

        //    if (WeekNumber > 52 && !ThursdayFlag)
        //        WeekNumber = 1;

        //    if (WeekNumber == 0)
        //        WeekNumber = WeekNumber_Entire4DayWeekRule(
        //        new DateTime(date.Year - 1, DEC, LASTDAYOFDEC));
        //    return WeekNumber;
        //}

        /// <summary>
        /// Metod som hämtar veckanummer utifrån specifikt datum
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>   
        public int WeekNrFromDate(DateTime date)
        {
            CultureInfo culture = CultureInfo.CurrentCulture;
            int weekNum = culture.Calendar.GetWeekOfYear(date, CalendarWeekRule.FirstFullWeek, DayOfWeek.Sunday);

            return weekNum;
        }

        /// <summary>
        /// Metod som hämtar datum för vald vecka
        /// </summary>
        /// <param name="year"></param>
        /// <param name="week"></param>
        /// <param name="dayOfWeek"></param>
        /// <returns></returns>
        public DateTime WeeknrToDate(int year, int week, DayOfWeek dayOfWeek) //hämtar datum för vald vecka
        {
            DateTime startDate = new DateTime(year, 1, 1);

            return startDate.AddDays((7 * week) + (int)dayOfWeek - (int)startDate.DayOfWeek);
        }




       
    }
}
